package com.epalmpay.controller.channel.datareport;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.datareport.MemberDataReportInDTO;
import com.epalmpay.dto.datareport.MemberDataReportListOutDTO;
import com.epalmpay.entity.Member;
import com.epalmpay.entity.StatisAgentDay;
import com.epalmpay.entity.StatisAgentMonth;
import com.epalmpay.entity.StatisGroupDay;
import com.epalmpay.entity.StatisGroupMonth;
import com.epalmpay.entity.StatisMemberDay;
import com.epalmpay.entity.StatisMemberMonth;
import com.epalmpay.entity.StatisShopDay;
import com.epalmpay.entity.StatisShopMonth;
import com.epalmpay.service.datareport.IMemberDataReportService;

/** 
 * 说明：数据报表 :会员数据报表
 * 创建人：gwn
 * 创建时间：2017-06-20
 */
@Controller
@RequestMapping(value="/merchant/memberDataReport")
public class MemberDataReportController  extends BaseController{
	
	@Resource
	private IMemberDataReportService memberDataReportService;
	
	
	@RequestMapping(value="/list")
	@ResponseBody
	public MemberDataReportListOutDTO List(MemberDataReportInDTO memberDataReportInDTO){
		
		MemberDataReportListOutDTO memberDataReportListOutDTO= new MemberDataReportListOutDTO();
		
		StatisAgentDay statisAgentDay=null;
		StatisShopDay statisShopDay=null;
		StatisShopMonth statisShopMonth=null;
		StatisAgentMonth statisAgentMonth=null;
		StatisMemberDay statisMemberDay = null;
		StatisMemberMonth statisMemberMonth = null;
		StatisGroupDay statisGroupDay =null;
		StatisGroupMonth statisGroupMonth = null;
		
		try {
			
			if (memberDataReportInDTO.getQueryDayOrMonth()==1) {
				if (memberDataReportInDTO.getJoinStart()==null) {
					Date date=new Date();//取时间
					Calendar calendar = new GregorianCalendar();
					calendar.setTime(date);
					calendar.add(calendar.DATE,-1);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
					date=calendar.getTime();
					memberDataReportInDTO.setJoinStart(date);
				}
			    statisAgentDay = memberDataReportService.queryStatisAgentDayCount(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisAgentDay(statisAgentDay);
				
				statisShopDay = memberDataReportService.queryStatisShopDayCount(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisShopDay(statisShopDay);
				
				statisMemberDay = memberDataReportService.queryStatisMemberDayCount(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisMemberDay(statisMemberDay);
				
				statisGroupDay = memberDataReportService.queryStatisGroupDayCount(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisGroupDay(statisGroupDay);
				
				
			}else{
				
			    statisAgentMonth = memberDataReportService.queryStatisAgentMonthCount(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisAgentMonth(statisAgentMonth);
				
				statisShopMonth = memberDataReportService.queryStatisShopMonthCount(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisShopMonth(statisShopMonth);
				
				statisMemberMonth = memberDataReportService.queryStatisMemberMonthCount(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisMemberMonth(statisMemberMonth);
				
				statisGroupMonth = memberDataReportService.queryStatisGroupMonthCount(memberDataReportInDTO);
				memberDataReportListOutDTO.setStatisGroupMonth(statisGroupMonth);
			}

			Member member = new Member();
			int totalCount = memberDataReportService.queryMemberCount(memberDataReportInDTO);
			member.setTotalCount(totalCount);
			memberDataReportListOutDTO.setMember(member);
			if (statisAgentDay!=null && statisShopDay!=null) {
				
				member.setConcernMemberNum(statisAgentDay.getConcernMemberNum()+statisShopDay.getConcernMemberNum());
				member.setPerfectMemberNum(statisAgentDay.getPerfectMemberNum()+statisShopDay.getPerfectMemberNum());
				member.setUnconcernMemberNum(statisAgentDay.getUnconcernMemberNum()+statisShopDay.getUnconcernMemberNum());
			}
			if (statisAgentMonth!=null && statisShopMonth!=null) {
				member.setConcernMemberNum(statisAgentMonth.getConcernMemberNum()+statisShopMonth.getConcernMemberNum());
				member.setPerfectMemberNum(statisAgentMonth.getPerfectMemberNum()+statisShopMonth.getPerfectMemberNum());
				member.setUnconcernMemberNum(statisAgentMonth.getUnconcernMemberNum()+statisShopMonth.getUnconcernMemberNum());
			}
			
			memberDataReportListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberDataReportListOutDTO;
		} catch (ApplicationException ae) { 
			memberDataReportListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}catch (Exception e) {
			memberDataReportListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
		}
		return memberDataReportListOutDTO;
		
	}
	

}
